package org.darkforest.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.RelationMapper;
import org.darkforest.model.Relation;
import org.darkforest.vo.MemberBase;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by pc on 2017/4/8.
 */
@Controller
@RequestMapping(value = "relation")
public class RelationController {
    @Autowired
    private RelationMapper relationMapper;

    @RequestMapping(value = "list")
    public ModelAndView list(String rName) {
        ModelAndView view = new ModelAndView("setting/relation/list");
        Relation relation = new Relation();
        relation.setrName(StringUtils.isBlank(rName)?null:rName);
        relation.setResultList(relationMapper.selectListByCondition(relation));
        view.addObject("page", relation);
        return view;
    }

    @RequestMapping(value = "toadd")
    public ModelAndView toAdd() {
        ModelAndView view = new ModelAndView("setting/relation/add");
        return view;
    }

    @RequestMapping(value = "doadd")
    @ArchivesLog(operationDescription = "新增关系")
    public String doAdd(Relation relation, HttpServletRequest request) {
        MemberBase memberBase = (MemberBase)request.getSession().getAttribute("memInfo");
        relation.setCreaterId(memberBase.getUserId());
        relation.setCreateName(memberBase.getUserName());
        relation.setCreateTime(DateUtil.toTimeString(new Date()));
        relationMapper.insert(relation);
        return "redirect:list";
    }

    @RequestMapping(value = "toedit")
    public ModelAndView toEdit(Integer id) {
        ModelAndView view = new ModelAndView("setting/relation/edit");
        Relation relation = relationMapper.selectByPrimaryKey(id);
        view.addObject("relation", relation);
        return view;
    }

    @RequestMapping(value = "doedit")
    @ArchivesLog(operationDescription = "修改关系")
    public String doEdit(Relation relation,HttpServletRequest request) {
        MemberBase memberBase = (MemberBase)request.getSession().getAttribute("memInfo");
        relation.setCreaterId(memberBase.getUserId());
        relation.setCreateName(memberBase.getUserName());
        relation.setCreateTime(DateUtil.toTimeString(new Date()));
        relationMapper.updateByPrimaryKeySelective(relation);
        return "redirect:list";
    }
}
